// index.js
// 获取应用实例
const app = getApp()
Page({
  data: {
    name: '',
    imgUrl: '',
    date: '',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
  },
  onLoad(options) {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true,
      })
    }
    let curTime = new Date()
    let year = curTime.getFullYear();
    let month = curTime.getMonth();
    let day = curTime.getDate();
    let hour = curTime.getHours();
    let min = curTime.getMinutes();
    let sec = curTime.getSeconds();
    let str = year + "-" + month + "-" + day + "    " + hour + ":" + min + ':' + sec

    this.setData({
      name: options.name,
      imgUrl: options.imgUrl,
      date: str,
    })

    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  onShareAppMessage: function () {
    let _this = this;
    return {
      title: '我已打卡红色地标' + '-' + _this.data.name,
      query: `name=${_this.data.name}&imgUrl=${_this.data.imgUrl}`,
      path: `/pages/share/share?name=${_this.data.name}&imgUrl=${_this.data.imgUrl}`,
    }
  },
  //发送到朋友圈
  onShareTimeline() {
    let _this = this;
    return {
      title: '我已打卡红色地标' + '-' + _this.data.name,
      query: `name=${_this.data.name}&imgUrl=${_this.data.imgUrl}`,
      imageUrl: '/statics/bg/share.png',
    }
  }
})